//
//  ZJDiscussSendMessageView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/9/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJDiscussSendMessageView.h"

@interface ZJDiscussSendMessageView ()<CSGrowingTextViewDelegate>

/** 分割线 */
@property (nonatomic, strong) UILabel *underline;
/** 输入框下划线 */
@property (nonatomic, strong) UILabel *textUnderLine;
@end

//最初高度默认49
@implementation ZJDiscussSendMessageView
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.growingTextView];
        [self addSubview:self.textUnderLine];
        [self addSubview:self.sender];
        [self addSubview:self.addButton];
        [self addSubview:self.underline];
        self.growingTextView.sd_layout.bottomSpaceToView(self, 7.5);
        
        
        self.textUnderLine.sd_layout
        .topSpaceToView(self.growingTextView, 0)
        .leftEqualToView(self.growingTextView)
        .widthRatioToView(self.growingTextView, 1)
        .heightIs(1);
        
        self.sender.sd_layout
        .topSpaceToView(self, 7.5)
        .leftSpaceToView(self.growingTextView, 10 * [UIScreen zj_screenWScale])
        .widthIs(50 * [UIScreen zj_screenWScale])
        .heightIs(34);
        
        self.addButton.sd_layout
        .topSpaceToView(self, 7.5)
        .leftSpaceToView(self.sender, 10 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self, 10 * [UIScreen zj_screenWScale])
        .heightIs(34);
        
        self.underline.sd_layout
        .topSpaceToView(self, 0)
        .leftSpaceToView(self, 0)
        .rightSpaceToView(self, 0)
        .heightIs(1);
        
    }
    return self;
}



- (void)action_button:(UIButton *)sender{
    if (self.delegate) {
        [self.delegate sendMessageWithView:self message:self.growingTextView.internalTextView.text];
    }
    self.growingTextView.internalTextView.text = @"";
}

- (void)action_addButton:(UIButton *)addbutton{
    [self.growingTextView.internalTextView endEditing:YES];
    self.addButton.selected = !self.addButton.selected;
    if (self.delegate) {
        [self.delegate sendMessageWithView:self showAddView:self.addButton.selected];
    }
}


#pragma mark - Setter
- (void)setDelegate:(id<CSGrowingTextViewDelegate,ZJSendMessageViewDelegate>)delegate{
    _delegate = delegate;
    self.growingTextView.delegate = delegate;
}

#pragma mark - Getters
- (CSGrowingTextView *)growingTextView{
    if (_growingTextView == nil){
        _growingTextView = [[CSGrowingTextView alloc] initWithFrame:CGRectMake(10 * [UIScreen zj_screenWScale], 7.5,[UIScreen zj_screenWith] - 124 * [UIScreen zj_screenWScale] , 34)];
        _growingTextView.placeholderLabel.text = @"来与大家一起讨论吧";
        _growingTextView.placeholderLabel.textColor = [UIColor lightGrayColor];
//        _growingTextView.backgroundColor = [UIColor viewBGGrayColor];
  
    }
    return _growingTextView;
}

- (UIButton *)sender{
    if (_sender == nil){
        _sender = [UIButton new];
        [_sender setTitle:@"发送" forState:UIControlStateNormal];
        _sender.backgroundColor = [UIColor themeColor];
        [_sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _sender.titleLabel.font = [UIFont subTitleTextFont];
        _sender.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_sender addTarget:self action:@selector(action_button:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sender ;
}

- (UIButton *)addButton{
    if (_addButton == nil){
        _addButton = [UIButton new];
        _addButton.selected = NO;
        [_addButton setImage:[UIImage imageNamed:@"discuss_add"] forState:UIControlStateNormal];
        [_addButton setImage:[UIImage imageNamed:@"discuss_add"] forState:UIControlStateSelected];
        [_addButton addTarget:self action:@selector(action_addButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

- (UILabel *)textUnderLine{
    if (_textUnderLine == nil){
        _textUnderLine  = [UILabel new];
        _textUnderLine.backgroundColor = [UIColor zj_colorWithHexString:@"a8a8a8"];
        
    }
    return _textUnderLine;
}

- (UILabel *)underline{
    if (_underline == nil){
        _underline = [UILabel new];
        _underline.backgroundColor = [UIColor viewBGGrayColor];
        
    }
    return _underline;
}
@end
